﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

using urBook.Engine;
using urBook.Engine.VcfUtilities;

namespace urBook.ConsoleUi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            AddressBook addressBook = new Engine.AddressBook();
            addressBook = Utilities.load("AddressBook.xml");
            List<ContactCard> duplicates = new List<ContactCard>();
            if (args.Length < 2)
            {
                args = new string[2];
                args[0] = "-help";
                args[1] = " ";
            }
            while (args[0] != "quit")
            {

                for (int i = 0; i < args.Length - 1; i += 2)
                {
                    string cmd = args[i];
                    string att = args[i + 1];

                    if (cmd == "-help")
                    {
                        Console.WriteLine(
                            "usage:   command attribute [value] ... \n" +
                            "-print   all | ( (contact | group) name)     prints target \n" +
                            "-load    file.xml                            loads file to database \n" +
                            "-save    file.xml                            saves database to file \n" +
                            "-import  file.vcf                            imports contacts to database \n" +
                            "-export  file.vcf                            exports database to vcf format \n" +
                            "-format  all | (contact name)                formats contact(s) values \n" +
                            "-append  all | (contact name) countryCode    append country code to contact(s) \n" +
                            "-find    duplicates | (contact name)         gets contact with given name \n" +
                            "-merge   duplicates                          merges duplicate contacts together \n" +
                            "quit                                         exits the application"
                            );
                    }
                    else if (cmd == "-print")
                    {
                        if (att == "all")
                            Console.WriteLine(addressBook.ToString());
                        else if (att == "contact")
                        {
                            string value = args[i + 2];
                            foreach(ContactCard c in addressBook.contacts.FindAll(x => x.name.formattedName.Contains(value)))
                            Console.WriteLine(c.ToString());
                            i++;
                        }
                        else if (att == "group")
                        {
                            string value = args[i + 2];
                            foreach(ContactCard c in addressBook.groups.Find(x => x.name == value).contacts)
                                Console.WriteLine(c.ToString());
                            i++;
                        }
                    }
                    else if (cmd == "-load")
                        addressBook = Utilities.load(att);
                    else if (cmd == "-save")
                        Utilities.save(addressBook, att);
                    else if (cmd == "-import")
                    {
                        Engine.AddressBook imported = new Engine.AddressBook();
                        imported = VcfManager.import(att);
                        addressBook.contacts.AddRange(imported.contacts);
                    }
                    else if (cmd == "-export")
                        VcfManager.export(addressBook, att);
                    else if (cmd == "-format")
                    {
                        if (att == "all")
                            addressBook.format();
                        else if (att == "contact")
                        {
                            string value = args[i + 2];
                            ContactCard c = addressBook.contacts.Find(x => x.name.formattedName.Equals(value));
                            c.format();
                            Console.WriteLine(c.ToString());
                            i++;
                        }
                    }
                    else if (cmd == "-append")
                        addressBook.appendCountryCode(att);
                    else if (cmd == "-find")
                    {
                        if (att == "duplicates")
                        {
                            duplicates = Utilities.findDuplicates(addressBook);
                            foreach(ContactCard c in duplicates)
                                Console.WriteLine(c.ToString());
                        }
                        else if (att == "contact")
                        {
                            string value = args[i + 2];
                            ContactCard c = addressBook.contacts.Find(x => x.name.formattedName.Contains(value));
                            if (c != null)    
                                Console.WriteLine(c.ToString());
                            i++;
                        }
                    }
                    else if (cmd == "-merge")
                    {
                        if (att == "duplicates")
                        {
                            ContactCard c = Utilities.merge(addressBook, duplicates);
                            Console.WriteLine(c);
                        }
                        else
                        {
                            Console.WriteLine("Please Check Command Syntax");
                        }
                    }
                    else
                        throw new Exception("Command Syntax not correct; type -help for a list of valid commands");
                }

                Console.Write("AddressBook:");
                string input = Console.ReadLine();
                args = input.Split(new char[] { ' ' });
            }
            /*
            addressBook = VcfManager.import("AddressBook.vcf");
            VcfManager.export(addressBook, "AddressBook2.vcf");

            Utilities.save(addressBook,"AddressBook.xml");
            addressBook = Utilities.load("AddressBook.xml");
            
            XmlSerializer serializer = new XmlSerializer(typeof(AddressBook.Engine.AddressBook));
            serializer.Serialize(Console.Out, addressBook);
            */
        }
    }
}

